package com.aq.appserver.server.theme.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.aq.appserver.server.account.dao.AccountDao;
import com.aq.appserver.server.account.dao.HeadImgDao;
import com.aq.appserver.server.account.entity.Account;
import com.aq.appserver.server.account.entity.HeadImg;
import com.aq.appserver.server.theme.bean.CommentBeanWeb;
import com.aq.appserver.server.theme.bean.ThemeBeanWeb;
import com.aq.appserver.server.theme.dao.CommentDao;
import com.aq.appserver.server.theme.dao.ThemeDao;
import com.aq.appserver.server.theme.entity.Comment;
import com.aq.appserver.server.theme.entity.Theme;
import com.aq.appserver.server.theme.service.ThemeService;

@Service
public class ThemeServiceImpl implements ThemeService{

	@Autowired
	private ThemeDao themeDao;
	
	@Autowired
	private CommentDao commentDao;
	
	@Autowired
	private AccountDao accountDao;
	
	@Autowired
	private HeadImgDao headImgDao;
	
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public String saveTheme(Theme theme) {
		Theme tempTheme = themeDao.save(theme);
	
		//更新用户羽毛数量
		accountDao.updateAccFeather(theme.getPhoneNum());
		return String.valueOf(tempTheme.getId());
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void saveContent(Comment content) {
		//更新用户羽毛数量
		accountDao.updateAccFeather(content.getPhoneNum());
		themeDao.updateThemeTime(Long.valueOf(content.getThemeId()), new Date());
		commentDao.save(content);
	}

	/**
	 * 更新点击量
	 */
	@Override
	@Transactional
	public void updateClickNum(String themeId) {
		themeDao.updateClickNum(Long.valueOf(themeId));	
	}

	@Override
	public Page<Theme> queryThemeList(String title, Pageable pageable) {
		return themeDao.findThemeByTitleLike(title, pageable);
	}

	@Override
	public Page<Theme> queryThemeListByPhoneNum(String phoneNum,
			String title, Pageable pageable) {
		return themeDao.findThemeByphoneNumAndTitleLike(phoneNum, title, pageable);
	}

	@Override
	public Page<Theme> queryThemeList(Pageable pageable) {
		return themeDao.findThemeList(pageable);
	}

	@Override
	public Page<Theme> queryThemeListByPhoneNum(String phoneNum,
			Pageable pageable) {
		return themeDao.findThemeByphoneNum(phoneNum, pageable);
	}

	@Override
	public int getReplyNum(String id) {
		return commentDao.getReplyNum(id);
	}

	@Override
	public int getMyHelpCount(String phoneNumber) {
		return themeDao.getMyHelpCount(phoneNumber);
	}

	@Override
	public int getMyReplyCount(String phoneNumber) {
		return commentDao.getMyReplyCount(phoneNumber);
	}

	@Override
	public List<HeadImg> queryHeadImgByPhoneNum(String phoneNum) {
		return headImgDao.findByPhoneNumber(phoneNum);
	}

	@Override
	public Page<Comment> findByThemeId(String themeId, Pageable pageable) {
		return commentDao.findContentByThemeId(themeId, pageable);
	}
	@Override
	public int queryWebThemeCount(ThemeBeanWeb theme) {
		return (int)themeDao.count();
	}
	
	@Override
	public List<ThemeBeanWeb> queryWebTheme(ThemeBeanWeb theme) {
		Pageable pageable = new PageRequest(theme.getPage()-1, theme.getRows(), new Sort(Direction.DESC, "topTime", "updateTime"));
		Iterable<Theme> it = themeDao.findAll(pageable);
		Iterator<Theme> itr = it.iterator();
		List<ThemeBeanWeb> list = new ArrayList<ThemeBeanWeb>();
		while(itr.hasNext())
		{
			ThemeBeanWeb bean = new ThemeBeanWeb();
			Theme one = itr.next();
			bean.setId(one.getId());
			bean.setContent(one.getContent());
			bean.setUrl(one.getUrl());
			bean.setTitle(one.getTitle());
			bean.setPhoneNum(one.getPhoneNum());
			bean.setReplyNum(getReplyNum(one.getId() + ""));
			bean.setTopTime(one.getTopTime());
			bean.setIsTop(one.getIsTop());
			bean.setFirstTime(one.getFirstTime());
			
			Account act = accountDao.findByPhoneNumber(one.getPhoneNum());
			bean.setNickName(act.getNickName());
			list.add(bean);
		}
		return list;
	}

	@Override
	@Transactional
	public void deleteWebTheme(ThemeBeanWeb theme) {
		String[] ids = theme.getIds().split(",");
		for(int i=0; i<ids.length; i++)
		{
			Theme tm = themeDao.findOne(Long.parseLong(ids[i]));
			commentDao.delete(commentDao.findByThemeId(ids[i]));
			themeDao.delete(Long.parseLong(ids[i]));
			String phoneNumber = tm.getPhoneNum();
			if(StringUtils.isEmpty(phoneNumber))
			{
				continue;
			}
			accountDao.updateAccFeather(phoneNumber, -1);
		}
	}
	
	@Override
	@Transactional
	public void toTopWebTheme(ThemeBeanWeb theme) {
		long id = theme.getId();
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		themeDao.updateTopTime(id, sd.format(new Date()));
		themeDao.updateTop(id, "1", sd.format(new Date()));
	}
	
	@Override
	@Transactional
	public void cancerTopWebTheme(ThemeBeanWeb theme) {
		long id = theme.getId();
		themeDao.updateTop(id, "0", null);
	}
	
	@Override
	public List<CommentBeanWeb> queryWebComment(CommentBeanWeb comment) {
		List<Comment> entityList = commentDao.findByThemeId(comment.getThemeId());
		List<CommentBeanWeb> list = new ArrayList<CommentBeanWeb>();
		for(Comment c : entityList)
		{
			CommentBeanWeb bean = new CommentBeanWeb();
			bean.setId(c.getId());
			bean.setContent(c.getContent());
			bean.setContentType(c.getContentType());
			bean.setPhoneNum(c.getPhoneNum());
			bean.setReplyTag(c.getReplyTag());
			list.add(bean);
		}
		return list;
	}

	@Override
	@Transactional
	public void deleteWebComment(CommentBeanWeb theme) {
		String[] ids = theme.getIds().split(",");
		for(int i=0; i<ids.length; i++)
		{
			Comment ct = commentDao.findOne(Long.parseLong(ids[i]));
			commentDao.delete(Long.parseLong(ids[i]));
			String phoneNumber = ct.getPhoneNum();
			if(StringUtils.isEmpty(phoneNumber))
			{
				continue;
			}
			accountDao.updateAccFeather(phoneNumber, -1);
		}
	}

	@Override
	public int queryTopNum() {
		return themeDao.queryTopNum();
	}

	@Override
	public Iterable<Theme> queryWebThemeList() {
		Sort sort = new Sort(Direction.DESC, "topTime", "updateTime");
		return themeDao.findAll(sort);
	}
}
